<?php
namespace frontend\controllers;
use app\models\User;
use app\models\CUser;
use Yii;

/**
 * Site controller
 */
class SiteController extends BaseController
{
    public function actions() {
        return [
            'captcha' =>  [
                'class' => 'yii\captcha\CaptchaAction',
                'height' => 50,
                'width' => 80,
                'minLength' => 4,
                'maxLength' => 4,
                'backColor' => 0X9a6c35
            ],
        ];
    }

    public function actionCaptcha(){
        $this->createAction('captcha')->getVerifyCode(TRUE);
    }

    public function actionChooseCompany(){
        $isUser=true;
        $this->layout="login";
        return $this->render("choose-company",['isUser'=>$isUser]);
    }
    public function actionLogin(){
        $isUser=false;
        $cookies = Yii::$app->response->cookies;
        $wrong=Yii::$app->session->get("wrong");
        $id=Yii::$app->request->get("company");

        //判断是否存在id
        if($id==""&&!isset($id)){
            $id=Yii::$app->request->cookies->getValue("companyId");

        }else{
            $cookies->add(new \yii\web\Cookie([
                'name' => 'companyId',
                'value' => $id,
                'expire'=>time()+3600*24*7
            ]));
        }

        $user= new User();
        $info=$user->findOne(['id'=>$id]);
        if($info!=null){
            Yii::$app->session->set("dbname","client".$info->id);

            $isUser=true;
        }
        $this->layout="login";
        if(!$isUser){
            return $this->render("choose-company",['isUser'=>true]);
        }
        return $this->render("login",['wrong'=>$wrong]);
    }

    public function actionCreateUser(){
        $user= new User();
        $user->username="test1";
        $user->password=Yii::$app->security->generatePasswordHash("test1");
        $user->companyname="测试1";
        $user->save();
        echo 1;
    }

    public function actionCreateClientUser(){
        $user= new CUser();
        $user->username="test1";
        $user->password=Yii::$app->security->generatePasswordHash("test1");
        $user->save();
        echo 1;
    }
    public function actionChooseLanguage(){
        $request = Yii::$app->request;
        $language=$request->get('language');
        $cookies = Yii::$app->response->cookies;
        $cookies->add(new \yii\web\Cookie([
            'name' => 'language',
            'value' => $language,
            'expire'=>time()+3600*24*7
        ]));

        $this->redirect(Yii::$app->request->getReferrer());
    }
    public function actionSignUp(){
        $request= Yii::$app->request;
        if($request->isAjax){
            $username=$request->post("username");
            $password=$request->post("password");
            $captcha=$request->post("captcha");
            $wrong = Yii::$app->session->get("wrong");
            $return = [];
            if($wrong){
                if(!$this->createAction('captcha')->validate($captcha,false)){
                    $return['status']=false;
                    $return['msg']=Yii::t('app','Invalid Captcha');
                    return json_encode($return);
                }

            }
            $userModel = new Cuser();
            $user = $userModel->findOne(['username'=>$username]);

            if($user){
                if(Yii::$app->security->validatePassword($password,$user->password)){
                    $return['status']=true;
                    Yii::$app->session->set("wrong",false);
                }else{
                    $return['status']=false;
                    $return['msg']=Yii::t('app','Incorrect password or username');
                    Yii::$app->session->set("wrong",true);
                }
            }else{
                    $return['status']=false;
                    $return['msg']=Yii::t('app','Incorrect password or username');
                    Yii::$app->session->set("wrong",true);

            }
            return json_encode($return);
        }

    }
}
